package com.bw.module_home.model.Adpter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bw.module_home.databinding.HomeTitleBinding
import com.bw.module_home.model.entity.SimpleTypeEntity

class HomeTitleApter():RecyclerView.Adapter<HomeTitleApter.HomeTitleViewHolder>() {

    interface OnItemClickListener{
        fun onItemClick(id:String)
    }

    private var listener : OnItemClickListener? = null

    fun setOnTypeItemClickListener(listener : OnItemClickListener){
        this.listener = listener
    }

    private val list = ArrayList<SimpleTypeEntity>()

    fun setData(mList: List<SimpleTypeEntity>){
        list.clear()
        list.addAll(mList)
        notifyDataSetChanged()
    }

    class HomeTitleViewHolder(
        val binding:HomeTitleBinding
    ) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeTitleViewHolder {
        // 加载列表项布局
        val binding = HomeTitleBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return HomeTitleViewHolder(binding)
    }

    override fun onBindViewHolder(holder: HomeTitleViewHolder, position: Int) {
        //数据绑定的优先级高于手动设置（框架会在生命周期中自动刷新绑定值）
        //直接给数据变量赋值，触发了数据绑定框架的自动同步机制
        val item = list[position]
        holder.binding.title = item
        // 给列表项设置点击事件
        // 2. 点击时传递当前项的pid
        holder.itemView.setOnClickListener {
            // 点击时回调，传入当前所有数据（用于新列表展示）
            listener?.onItemClick(item.channelid)
        }
    }

    // 获取数据总数
    override fun getItemCount(): Int {
        return list.size
    }

}